public class CountArrangement {

    int ans = 0;
    boolean[] flag = new boolean[15];
    public int countArrangement(int n) {
        process(0, n);
        return ans;
    }

    public void process(int i, int n) {
        if (i == n) {
            ans++;
            return;
        }

        for (int j = 0; j < n; j++) {
            if (!flag[j] && isVaild(i + 1, j + 1)) {
                    flag[j] = true;
                    process(i + 1, n);
                    flag[j] = false;
            }
        }
    }

    public boolean isVaild(int i, int j) {
        if (i % j == 0 || j % i == 0) return true;
        return false;
    }
}
